# from PyGLES (by renaud blanch)
import objc
from uikit import NSString, UIKit, NSObject
from ctypes import c_char_p, addressof

objc.loadBundleFunctions(UIKit, globals(),
                         [("UIApplicationMain", "iii@@"),]
                         )

_UIApplicationMain = UIApplicationMain

def UIApplicationMain(argv=None, principal=objc.nil, delegate=None):
    """
    UIApplicationMain convenient wrapper (try to be more pythonic).

    usage: UIApplicationMain(delegate=Delegate)
    """
    if argv is None:
        from sys import argv

    argc = len(argv)
    argv = (c_char_p * argc)(*argv)
	
    if principal != objc.nil:
        principal = NSString.alloc().initWithString_(principal.__name__)

    if delegate is None:
        delegate = principal
    elif delegate != objc.nil:
        delegate = NSString.alloc().initWithString_(delegate.__name__)
	
    return _UIApplicationMain(argc, addressof(argv), principal, delegate)


class Delegate(NSObject):
    """Base class for application delegates"""
    # possible delegate instance methods (add _)
    # - (void)applicationDidFinishLaunching:(UIApplication *)application;
    # - (void)applicationDidBecomeActive:(UIApplication *)application;
    # - (void)applicationWillResignActive:(UIApplication *)application;
    # - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url;  // no equiv. notification. return NO if the application can't open for some reason

    # - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application;      // try to clean up as much memory as possible. next step is to terminate app
    # - (void)applicationWillTerminate:(UIApplication *)application;
    # - (void)applicationSignificantTimeChange:(UIApplication *)application;        // midnight, carrier time update, daylight savings time change

    # - (void)application:(UIApplication *)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation duration:(NSTimeInterval)duration;
    # - (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation;

    # - (void)application:(UIApplication *)application willChangeStatusBarFrame:(CGRect)newStatusBarFrame;   // in screen coordinates
    # - (void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame;



